package com.ihai.spring.boot.sample.distributed.counter.util;

import com.fasterxml.jackson.core.io.DataOutputAsStream;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONWrappedObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import java.io.*;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

/**
 * DESC
 *
 * @author 张驰
 * @date 2019/12/24
 */
public class HttpUtils {
    private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
    private static CloseableHttpClient httpClient;
    static {
        try {
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(300000)
                    .setSocketTimeout(300000).build();
            httpClient = HttpClientBuilder.create()
                    .setMaxConnPerRoute(10000)
                    .setMaxConnTotal(50000)
                    .setSSLContext(SSLContext.getDefault())
                    .setDefaultRequestConfig(requestConfig)
                    .build();
        } catch (NoSuchAlgorithmException e) {
        }

    }

    public static InputStream post(String url, Object params){
        try {
            HttpUriRequest request = null;
            if(params != null){
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ObjectOutput oo = new ObjectOutputStream(byteArrayOutputStream);
                oo.writeObject(params);
                HttpEntity httpEntity = new ByteArrayEntity(byteArrayOutputStream.toByteArray(), ContentType.APPLICATION_JSON);
                request =RequestBuilder.post(url)
                    .setEntity(httpEntity)
                    .build();
            }else{
                request =RequestBuilder.post(url)
                        .setHeader("ContentType",ContentType.APPLICATION_JSON.toString())
                        .build();
            }
            CloseableHttpResponse response = httpClient.execute(request);
            return response.getEntity().getContent();
        }catch (Exception e){
            logger.error("post to ["+url+"] failed.", e);
            return null;
        }
    }

    public static InputStream get(String url){
        try {
            HttpUriRequest request = RequestBuilder.get(url)
                    .setHeader("ContentType",ContentType.APPLICATION_JSON.toString())
                    .build();
            CloseableHttpResponse response = httpClient.execute(request);
            return response.getEntity().getContent();
        }catch (Exception e){
            logger.error("post to ["+url+"] failed.", e);
            return null;
        }
    }
}
